package com.wxxymaker.edu.service;

import com.blade.ioc.annotation.Bean;
import com.blade.kit.CollectionKit;
import com.wxxymaker.edu.model.entity.Comment;
import com.wxxymaker.edu.model.entity.Course;
import com.wxxymaker.edu.model.entity.User;
import com.wxxymaker.edu.utils.TimeUtils;
import io.github.biezhi.anima.Anima;
import io.github.biezhi.anima.core.AnimaQuery;
import io.github.biezhi.anima.core.Joins;
import io.github.biezhi.anima.enums.OrderBy;
import io.github.biezhi.anima.page.Page;
import lombok.extern.slf4j.Slf4j;

/**
 * @AUTHOR soft
 * @DATE 2018/9/9 20:27
 * @DESCRIBE
 */
@Slf4j
@Bean
public class CommentService implements Service<Comment> {
    @Override
    public Comment add(Comment comment) {
        comment.setTime(TimeUtils.getTime());
        boolean rollback = Anima.atomic(() -> {
            comment.setId(comment.save().asInt());
        }).catchException(e -> {
            log.error("保存失败！{}", e.getMessage());
        }).isRollback();
        return rollback ? null : comment;
    }

    @Override
    public Comment delete(Object pk) {
        Comment comment = byId(pk);
        int delete = comment.delete();
        if (delete > 0) {
            return comment;
        }
        return null;
    }

    public boolean delete( Integer id){
        boolean rollback = Anima.atomic(()->{
            AnimaQuery<Comment> commentAnimaQuery = Anima.delete().from(Comment.class);
            if (id!=null){
                commentAnimaQuery.where(Comment::getId, id);
                commentAnimaQuery.execute();
            }
        }).catchException(e->{
            log.error("删除评价失败！{}",e.getMessage());
        }).isRollback();
        return !rollback;
    }

    @Override
    public boolean change(Comment comment) {
        return false;
    }

    @Override
    public Comment byId(Object pk) {
        return null;
    }

    public Page<Comment> page(Integer kid,Integer page, Integer size) {
        Page<Comment> page1 = Anima.select().from(Comment.class)
                .join(Joins.with(User.class)
                        .as(Comment::getUser)
                        .on(Comment::getUserId, User::getId))
                .join(Joins.with(Course.class)
                        .as(Comment::getCourse)
                        .on(Comment::getCourseId,Course::getId))
                .where(Comment::getCourseId, kid)
                .order(Comment::getTime, OrderBy.DESC)
                .page(page, size);
        if (page1.getTotalRows() > 0) {
            return page1;
        }
        return null;
    }

    public Page<Comment> pages(Integer uid,Integer kid,Integer page, Integer size) {
        AnimaQuery<Comment> query=Anima.select().from(Comment.class)
                .join(Joins.with(User.class)
                        .as(Comment::getUser)
                        .on(Comment::getUserId, User::getId))
                .join(Joins.with(Course.class)
                        .as(Comment::getCourse)
                        .on(Comment::getCourseId,Course::getId))
                .join(Joins.with(Course.class)
                        .as(Comment::getCourse)
                        .on(Comment::getCourseId,Course::getId));

        if (uid!=null) query.where(Comment::getUserId, uid);
        if (kid!=null) query.where(Comment::getCourseId, kid);

        query.order(Comment::getTime, OrderBy.DESC);
        Page<Comment> page1 = query.page(page, size);
        if (CollectionKit.isNotEmpty(page1.getRows())) {
            return page1;
        }
        return null;
    }

    public long countBykId(Integer courseId) {
        return Anima.select().from(Comment.class).where(Comment::getCourseId, courseId).count();
    }

    public Long countByUid(Integer userId) {
        return Anima.select().from(Comment.class).where(Comment::getUserId, userId).count();
    }

    public int deleteByKid(Integer kid) {
        return Anima.delete().from(Comment.class)
                .where(Comment::getCourseId, kid)
                .execute();
    }
}
